package edu.sctu.jarime.june.day20150623.util;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import edu.sctu.jarime.june.day20150623.appbean.JsonAppbean;
import edu.sctu.jarime.june.day20150623.appbean.ListAppbean;

/**
 * Created by user19 on 2015/6/23.
 */
public class JsonUtil {
    /***
     * 解析Json文本 ，并且将对象传出
     * @param json
     * @return
     */
    //利用第三方包解析
    public static JsonAppbean setJson(String json) {

        JsonAppbean jsonAppbean = new JsonAppbean();
        JSONObject jsonObject = JSON.parseObject(json);
        jsonAppbean.error = jsonObject.getIntValue("error");
        jsonAppbean.message = jsonObject.getString("message");

        String list = jsonObject.getString("list");
        JSONArray jsonArray = JSON.parseArray(list);


        for (int i = 0; i < jsonArray.size() ; i++) {

            ListAppbean listappbean  = new ListAppbean();
            listappbean.rec_id = jsonArray.getJSONObject(i).getString("rec_id");
            listappbean.goods_id = jsonArray.getJSONObject(i).getString("goods_id");
            listappbean.category_id = jsonArray.getJSONObject(i).getString("category_id");
            listappbean.brand_id = jsonArray.getJSONObject(i).getString("brand_id");
            listappbean.goods_name = jsonArray.getJSONObject(i).getString("goods_name");
            listappbean.goods_price = jsonArray.getJSONObject(i).getString("goods_price");
            listappbean.goods_introduce = jsonArray.getJSONObject(i).getString("goods_introduce");

            jsonAppbean.list.add(listappbean);

        }

        return jsonAppbean;

    }



    //快速解析方法
    public static JsonAppbean parsingJson(String json){
         JsonAppbean jsonAppbean = JSONObject.parseObject(json, JsonAppbean.class);
        return jsonAppbean;

    }
}
